(function() {
    angular.module('BlurAdmin.pages.UserlistM')
        .controller('newSystemCtrl', newSystemCtrl);

    function newSystemCtrl($scope, $openModal, $http, $myconfig, tongxin, $mydialog) {

        var mobile = tongxin.getUserInfo();
        console.log(user.user.role);
        var role = [{ name: 'runner', val: '运营人员' }];

        if (user.user.role == 'superadmin') {
            role = [{ name: 'admin', val: '系统管理员' },
                { name: 'runner', val: '运营人员' }
            ];
        }

        $scope.role = role;

        $scope.user_opt = {
            "mobile": "",
            "email": "",
            "role": "runner",
            "username": "",
            "parentid": user.user.id, //操作人员的id
            "parentname": user.user.username, //操作人员的昵称
            "desc": ''
        };

        //检测条件是否满足
        $scope.add_user = function() {
            var opt = $scope.user_opt;
            if (!opt.username) {
                return $mydialog.fail("请设置用户昵称");
            }
            if (!opt.mobile) {
                return $mydialog.fail("请设置手机号");
            }
            var myreg = /^[1]\d{10}$/;
            if (!myreg.test(opt.mobile)) {
                return $mydialog.fail('请输入有效的手机号码');
            }
            if (!opt.email) {
                return $mydialog.fail("请设置用户的邮箱");
            }
            var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
            if (!re.test(opt.email)) {
                return $mydialog.fail("请设置正确的邮箱");
            }

            if (!opt.desc) {
                return $mydialog.fail("请设置用户描述");
            }

            if (mobile) { //如果有手机号则是编辑用户信息
                update_admin()
            } else { //新增用户
                adduser();
            }
        };

        //新增系统用户
        function adduser() {
            $http.post($myconfig.api.users_users_add_admin, $scope.user_opt)
                .then(function(res) {
                    console.log(res);
                    if (res.data.success) {
                        $mydialog.success("设置成功");
                        $scope.Search();
                        $scope.myopen.opt_close();
                    } else {
                        $mydialog.fail(res.data.data);
                    }
                })
        }

        //获取用户信息
        function getuser() {
            $http.get($myconfig.api.users_users_admin_info, {
                params: {
                    mobile: mobile
                }
            }).then(function(res) {
                $scope.user_opt = res.data.data;
            })
        };
        mobile && getuser();

        function update_admin() {
            $scope.user_opt.admin_id = $scope.user_opt.id;
            $scope.user_opt.admin_name = $scope.user_opt.username;
            $http.post($myconfig.api.users_users_update_admin, $scope.user_opt)
                .then(function(res) {
                    console.log(res);
                    if (res.data.success) {
                        $mydialog.success("设置成功");
                        $scope.Search();
                        $scope.myopen.opt_close();
                    } else {
                        $mydialog.fail(res.data.data);
                    }
                })
        }

    }
})();